/**
 * Created by Administrator on 2017/8/7.
 */

$(window).load(function () {
    drag();
    $(".fwr_toobar_handtool").addClass("fwr_toobar_handtool_active");


    // 懒加载

});

$(function () {
    var $MainFrame = $("#MainFrame");
    var $fxtree_panel = $("#fxtree_panel");
    var $left_panel1_title = $("#left_panel1_title");
    var $pdf_left_nav = $("#pdf_left_nav");
    // 左侧菜单栏
    var $pdf_left_navbar = $("#pdf_left_navbar");
    var $pdf_scroll_pane = $("#pdf_scroll_pane");
    var $jspContainer = $("#jspContainer");
    // 适合宽度的按钮
    var $btnFitWidth = $("#btnFitWidth");
    var $jspPane = $("#jspPane");
    var $jspPane_scroll=$("#jspPane_scroll");
    // 窗口大小
    var $window_w = $(window).width();
    var $window_h = $(window).height();
    //alert($window_h);
    var pdf_left_nav_width = $pdf_left_nav.width() + $fxtree_panel.width();
    var pdf_left_nav_height = $window_h - 46;
    var $fwr_page_bg_image = $(".fwr_page_bg_image");
    // 左侧缩略图div
    var $small_img=$(".skeletonize-img-con");
    // 缩小的按钮
    var $btnzoomOut = $("#btnzoomOut");
    // 放大的按钮
    var $btnzoomIn = $("#btnzoomIn");
    // 选择显示的百分比
    var $zoomSel = $("#zoomSel");
    // 小滚动条的限制con
    var $skeletonize_list = $("#skeletonize_list");
    $skeletonize_list.height(pdf_left_nav_height - 14);
    // 获取缩略图div
    $skeletonize_img_con = $(".skeletonize-img-con");


    // 给最外层祖div加高度
    $MainFrame.height($window_h);
    // 给左侧菜单增加高度
    $pdf_left_navbar.height(pdf_left_nav_height);

    // 初始化给右侧内容增加宽度高度
    var rt_con_w = $window_w - $pdf_left_nav.width();
    $jspContainer.width(rt_con_w);
    $jspContainer.height(pdf_left_nav_height);
    $pdf_scroll_pane.width(rt_con_w);
    $pdf_scroll_pane.height(pdf_left_nav_height);
    $jspPane.width(rt_con_w);
    $jspPane.height(pdf_left_nav_height);
    $fxtree_panel.height(pdf_left_nav_height);

    // 动态获取滚动条高度并且根据高度控制按钮显示隐藏以及第几页操作
    var max_page = 4; //最大页数
    var cur_page = 1; // 当前页
    var pre_page = 1; // 之前页
    var margin_px = 20; // 页之间的距离
    var marggin_px = 14; // 页之间的距离
    var beiShu=0.78;

    $jspPane.scroll(function () {
        //alert(1);
        var pageSort_judge = $fwr_page_bg_image_h * beiShu;
        var $this_h = $(this).scrollTop();
        //console.log($this_h);
        var totalheight = parseFloat($jspPane.height()) + parseFloat($this_h);
        var documentheight = parseFloat($jspPane_scroll.height());
        // 控制首页前一页按钮的显示隐藏
        if ($this_h < pageSort_judge) {
            $("#btnfirst").addClass("toobar-btn-first-dis");
            $("#btnpre").addClass("toobar-btn-pre-dis");
        } else {
            //alert(2)
            $("#btnfirst").removeClass("toobar-btn-first-dis");
            $("#btnpre").removeClass("toobar-btn-pre-dis");
        }
        // 控制后页末页按钮的显示隐藏
        if ((documentheight - totalheight) <= pageSort_judge) {
            $("#btnnext").addClass("toobar-btn-next-dis");
            $("#btnlast").addClass("toobar-btn-last-dis");
        } else {
            $("#btnnext").removeClass("toobar-btn-next-dis");
            $("#btnlast").removeClass("toobar-btn-last-dis");
        }
        pre_page = cur_page;
        // 根据滚动条高度算出当前pdf是第几页
        for (var i = cur_page - 1; i <= cur_page + 1; i++) {
            if (i == 0) {
                continue;
            } else if (i == 1) {
                if ($this_h >= 0 && $this_h < parseInt(beiShu * $fwr_page_bg_image_h)) {
                    cur_page = i;
                    break;
                }
            } else if (i == max_page) {
                if ($this_h >= parseInt((i - 2 + beiShu) * $fwr_page_bg_image_h + (i - 2) * margin_px)) {
                    cur_page = i;
                }
                break; // 不管是否找到都结束循环
            } else {
                if ($this_h >= parseInt((i - 2 + beiShu) * $fwr_page_bg_image_h + (i - 2) * margin_px)
                    && $this_h < parseInt((i - 1 + beiShu) * $fwr_page_bg_image_h + (i - 1) * margin_px)) {
                    cur_page = i;
                    break;
                }
            }
        }

        // 分页
        //alert("滚动里面的："+cur_page)
        $("#textpagenum").val(cur_page);
        if (pre_page != cur_page) {
            $skeletonize_list.scrollTop(179 * (cur_page - 1));
        }

        // 左侧选中随着右侧滚动而滚动
        var $select_on_small_img=$small_img.eq(cur_page-1);
        $select_on_small_img.addClass("skeletonize-img-con-on").siblings().removeClass("skeletonize-img-con-on");
        $small_img.find("img").removeClass("skeletonize-img-on");
        $select_on_small_img.find("img").addClass("skeletonize-img-on");
    });

    // 点击左侧缩略图右侧滚动显示对应的大图
    $small_img.click(function () {
        var $small_img_index=$(this).index();
        $(this).addClass("skeletonize-img-con-on").siblings().removeClass("skeletonize-img-con-on");
        $small_img.find("img").removeClass("skeletonize-img-on");
        $(this).find("img").addClass("skeletonize-img-on");

        cur_page=$small_img_index+1;
        //alert(cur_page)

        $("#textpagenum").val(cur_page);
        if(cur_page<=1){
            $jspPane.scrollTop(0);
        }else{
            $jspPane.scrollTop((cur_page-2+beiShu)*$fwr_page_bg_image_h+(cur_page-2)*margin_px);
        }
    });


    // 获取每个pdf文件图片宽高，给图片外层div加宽度fwr_page_bg_image
    var $fwr_page_bg_image_w = $fwr_page_bg_image.eq(0).width();
    var $fwr_page_bg_image_h = $fwr_page_bg_image.eq(0).height();
    var $fwr_page = $(".fwr_page");
    var fwr_page_wrap = $(".fwr_page_wrap");
    var fwr_page_len = $fwr_page.length;
    var f_div_w = $window_w - $pdf_left_navbar.width();
    var $fwr_page_w = (f_div_w - $fwr_page_bg_image_w) / 2;
    setImageWidth($fwr_page, fwr_page_len, $fwr_page_bg_image_w, $fwr_page_bg_image_h, $fwr_page_w);


    // 点击缩小按钮
    var selectVal = $zoomSel.find("option:selected").val();
    var select_index = $zoomSel.find("option:selected").index();
    var selectVal_before;
    //alert(multiple);

    // 点击缩小按钮
    $btnzoomOut.click(function () {
        if (select_index > 0) {
            select_index--;
            //alert(select_index);
            if (select_index == 3) {
                // 适合宽度的图片并且使得图片居中
                var multi = (rt_con_w * 0.97 / $fwr_page_bg_image_w);
                // alert(multi);
                $fwr_page_bg_image_w = rt_con_w * 0.97;
                $fwr_page_bg_image_h = $fwr_page_bg_image_h * multi;
                $fwr_page_w = (f_div_w - $fwr_page_bg_image_w) / 2;
                setImageWidth($fwr_page, fwr_page_len, $fwr_page_bg_image_w, $fwr_page_bg_image_h, $fwr_page_w);
                $btnFitWidth.addClass("toolbar-fitwidth-on");
            } else {

                selectVal_before = selectVal;
                //selectVal_now=selectVal-50;
                selectVal = parseInt(selectVal) - 50;
                //alert(selectVal_before+"****"+selectVal);
                var decMultiple = selectVal / selectVal_before;
                // 缩小图片并且使得图片居中
                $fwr_page_bg_image_w = $fwr_page_bg_image_w * decMultiple;
                $fwr_page_bg_image_h = $fwr_page_bg_image_h * decMultiple;
                $fwr_page_w = (f_div_w - $fwr_page_bg_image_w) / 2;
                setImageWidth($fwr_page, fwr_page_len, $fwr_page_bg_image_w, $fwr_page_bg_image_h, $fwr_page_w);
                $btnFitWidth.removeClass("toolbar-fitwidth-on");
            }


            // 同时改变select的选中值
            $zoomSel.find("option").eq(select_index).prop("selected", true);
            $jspPane.scrollTop((cur_page - 2 + beiShu) * $fwr_page_bg_image_h + (cur_page - 2) * marggin_px);

        } else {
            alert(" 已经是最小");
        }


    });

    // 点击放大图片
    $btnzoomIn.click(function () {
        if (select_index < 4) {
            select_index++;
            //alert(select_index);
            if (select_index == 3) {
                // 适合宽度的图片并且使得图片居中
                var multi = (rt_con_w * 0.97 / $fwr_page_bg_image_w);
                //alert(multi);
                $fwr_page_bg_image_w = rt_con_w * 0.97;
                $fwr_page_bg_image_h = $fwr_page_bg_image_h * multi;
                $fwr_page_w = (f_div_w - $fwr_page_bg_image_w) / 2;
                setImageWidth($fwr_page, fwr_page_len, $fwr_page_bg_image_w, $fwr_page_bg_image_h, $fwr_page_w);
                $btnFitWidth.addClass("toolbar-fitwidth-on");
            } else {
                selectVal_before = selectVal;
                selectVal = parseInt(selectVal) + 50;
                //alert(selectVal)
                var incMultiple = selectVal / selectVal_before;
                //alert(incMultiple);
                // 放大图片并且使得图片居中
                $fwr_page_bg_image_w = $fwr_page_bg_image_w * incMultiple;
                $fwr_page_bg_image_h = $fwr_page_bg_image_h * incMultiple;
                $fwr_page_w = (f_div_w - $fwr_page_bg_image_w) / 2;
                setImageWidth($fwr_page, fwr_page_len, $fwr_page_bg_image_w, $fwr_page_bg_image_h, $fwr_page_w);
                $btnFitWidth.removeClass("toolbar-fitwidth-on");
            }
            // 同时改变select的选中值
            $zoomSel.find("option").eq(select_index).prop("selected", true);
            $jspPane.scrollTop((cur_page - 2 +beiShu) * $fwr_page_bg_image_h + (cur_page - 2) * marggin_px);

        } else {
            alert(" 已经是最大");
        }
    });


    // 点击适合宽度的按钮
    $btnFitWidth.click(function () {
        // 适合宽度的图片并且使得图片居中
        var multi = (rt_con_w * 0.97 / $fwr_page_bg_image_w);
        //alert(multi);
        $fwr_page_bg_image_w = rt_con_w * 0.97;
        $fwr_page_bg_image_h = $fwr_page_bg_image_h * multi;
        $fwr_page_w = (f_div_w - $fwr_page_bg_image_w) / 2;
        setImageWidth($fwr_page, fwr_page_len, $fwr_page_bg_image_w, $fwr_page_bg_image_h, $fwr_page_w);
        $btnFitWidth.addClass("toolbar-fitwidth-on");
        select_index = 3;
        selectVal = 200;
        $zoomSel.find("option").eq(select_index).prop("selected", true);
        $jspPane.scrollTop((cur_page - 2 + beiShu) * $fwr_page_bg_image_h + (cur_page - 2) * marggin_px);
    });

    // 点击展开缩略图

    $("#btnList li").click(function () {
        var $index = $(this).index();
        // 控制显示隐藏
        $fxtree_panel.show();
        if ($index == 0 || $index == 1) {
            $(".pubDiv").hide();
            $(".pubDiv").eq(0).show();
            $(".left_panel").removeClass("left_panel-on");
            if ($index == 0) {
                $(this).next().addClass("left_panel-on");
            } else {
                $(this).addClass("left_panel-on");
            }
            $left_panel1_title.find("span").html("缩略图");
        } else if ($index == 2) {
            $(".pubDiv").hide();
            $(".pubDiv").eq(1).show();
            $(".left_panel").removeClass("left_panel-on");
            $(this).addClass("left_panel-on");
            $left_panel1_title.find("span").html("书签");
        }
        // else{
        //     $(".pubDiv").hide();
        //     $(".pubDiv").eq(2).show();
        //     $(".left_panel").removeClass("left_panel-on");
        //     $(this).addClass("left_panel-on");
        // }

        // 控制展开的宽度
        $pdf_left_navbar.width(pdf_left_nav_width);
        // 控制右侧内容的宽度和位置
        rt_con_w = $window_w - pdf_left_nav_width;
        //alert(rt_con_w)
        $pdf_scroll_pane.css({
            "width": rt_con_w + "px",
            "left": pdf_left_nav_width + "px"
        });
        $jspContainer.css({
            "width": rt_con_w + "px"
        });
        $jspPane.css({
            "width": rt_con_w + "px"
        });
        $jspPane_scroll.css({
            "width": rt_con_w + "px"
        });
        $fwr_page.css({
            "width": rt_con_w + "px"
        });


        // 展开缩略图以后重新计算图片的margin-left
        f_div_w = $window_w - pdf_left_nav_width;
        $fwr_page_w = (f_div_w - $fwr_page_bg_image_w) / 2;
        setImageWidth($fwr_page, fwr_page_len, $fwr_page_bg_image_w, $fwr_page_bg_image_h, $fwr_page_w);
    });
    // $("#leftpopimg").click(function () {
    //          $fxtree_panel.removeClass("hideClass");
    //          $pdf_left_navbar.width(pdf_left_nav_width);
    //     $pdf_scroll_pane.css({
    //         "width":$window_w-pdf_left_nav_width+"px",
    //         "left":pdf_left_nav_width+"px"
    //     });
    // });

    // 点击收起缩略图

    $("#pac_up_btn").click(function () {
        $fxtree_panel.hide();
        $pdf_left_navbar.width($pdf_left_nav.width());
        // 控制右侧内容的宽度和位置
        rt_con_w = $window_w - $pdf_left_nav.width();
        $pdf_scroll_pane.css({
            "width": rt_con_w + "px",
            "left": $pdf_left_nav.width() + "px"
        });
        $jspContainer.css({
            "width": rt_con_w + "px"
        });
        $jspPane.css({
            "width": rt_con_w + "px"
        });
        $jspPane_scroll.css({
            "width": rt_con_w + "px"
        });
        $fwr_page.css({
            "width": rt_con_w + "px"
        });


        // 收起缩略图以后重新计算图片的margin-left
        f_div_w = $window_w - $pdf_left_nav.width();
        $fwr_page_w = (f_div_w - $fwr_page_bg_image_w) / 2;
        setImageWidth($fwr_page, fwr_page_len, $fwr_page_bg_image_w, $fwr_page_bg_image_h, $fwr_page_w);
    });


    function setImageWidth(obj, page_len, img_w, img_h, img_margin_lf) {
        for (var i = 0; i < page_len; i++) {
            var $this = obj.eq(i);
            var $fwr_page_bg_image = $this.find(".fwr_page_bg_image");
            // $this.css({
            //     "width": img_w + "px",
            //     "height": img_h + "px",
            //     "margin-left": img_margin_lf + "px"
            // });

            $this.find(".fwr_page_wrap").css({
                "width": img_w + "px",
                "height": img_h + "px",
                "margin-left": img_margin_lf + "px"
            });

            $fwr_page_bg_image.css({
                "width": img_w + "px",
                "height": img_h + "px"
            });
        }
    }


    // 缩略图选中事件
    $skeletonize_img_con.click(function () {
        $(this).addClass("skeletonize-img-con-on").siblings().removeClass("skeletonize-img-con-on");
        var $this_img_sm = $(this).find("img");
        $(".skeletonize-img-con img").removeClass("skeletonize-img-on");
        $this_img_sm.addClass("skeletonize-img-on");
    });



});